package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class GamehallPage extends Utils {
    public static String url = "http://8.156.68.126:8080/game_hall.html";

    public GamehallPage() {
        super(url);
    }

    //模拟第二个玩家
    public void addUser(){
        createSecondDriver();

        try {
            // 第二个用户访问登录页面
            secondDriver.get("http://8.156.68.126:8080/login.html");

            secondWait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".toast.Andu")));
            Thread.sleep(300);

            // 第二个用户登录
            // 增加更明确的等待条件
            secondWait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#username")));
            secondDriver.findElement(By.cssSelector("#username")).sendKeys("Andu"); // 替换为第二个用户的用户名
            secondDriver.findElement(By.cssSelector("#password")).sendKeys("123456"); // 替换为第二个用户的密码
            secondDriver.findElement(By.cssSelector("#submit")).click();

            // 等待登录完全成功，增加更多检查点
            secondWait.until(ExpectedConditions.urlContains("game_hall"));
            secondWait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#user-stats")));

            // 增加匹配前的稳定等待
            Thread.sleep(1000); // 适当增加等待时间
            secondWait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#match-button")));

            // 第二个用户点击匹配
            secondDriver.findElement(By.cssSelector("#match-button")).click();

        } catch (Exception e) {
            e.printStackTrace();
            // 确保在出现异常时关闭第二个driver
            secondDriver.quit();
        }
    }

    //检查页面是否可以正常打开
    public void checkPageRight() throws InterruptedException {
        //检查模态窗元素是否加载成功
        driver.findElement(By.cssSelector("body > div > main > div"));
        //检查占位符
        driver.findElement(By.cssSelector("body > div > main > div > div.user-info > div.user-avatar-placeholder"));
        //检查用户信息
        driver.findElement(By.cssSelector("#user-stats"));
        //获取玩家分数
        initScore = driver.findElement(By.cssSelector("#user-stats > div:nth-child(2) > span.stat-value")).getText();
        //检查匹配按钮
        driver.findElement(By.cssSelector("#match-button"));
        //检查排行榜
        driver.findElement(By.cssSelector("#leaderboard-button"));
    }

    //检查排行榜
    public void checkRanking() throws InterruptedException {
        driver.findElement(By.cssSelector("#leaderboard-button")).click();

        //检查玩家信息加载
        driver.findElement(By.cssSelector("#leaderboard-list"));
        driver.findElement(By.cssSelector("#leaderboard-list > div:nth-child(1)")).click();
        driver.findElement(By.cssSelector("#leaderboard-list > div:nth-child(2)")).click();
        driver.findElement(By.cssSelector("#leaderboard-list > div:nth-child(3)")).click();

        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#leaderboard-modal > div > button")).click();
    }

    //开始匹配
    public void clickMatch(){
        driver.findElement(By.cssSelector("#match-button")).click();
        wait.until(ExpectedConditions.urlContains("game_room"));
    }
}
